﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Apps.BLL.SelfRcRelated;
using Apps.BLL.YANX;
using Apps.Common;
using Apps.Models;
using Apps.Web.Core;
using static java.util.concurrent.locks.ReentrantReadWriteLock;

namespace Apps.Web.Controllers
{
    public class ErpController : Controller
    {
        BLL.ERP.ERP_BLL bLL = new BLL.ERP.ERP_BLL();
        YANX_RECORD_BLL yxBll = new YANX_RECORD_BLL();
        SelfRcRelatedBLL relatedBLL = new SelfRcRelatedBLL();
        //[SupportFilter]
        [HttpGet]
        public JsonResult GetSamplingData(string key, string samplingStartTime, string samplingEndTime, string cph = "", string sparestr1 = "")
        {
            //return Json(new { code = -1, msg = "接口暂未开放" }, JsonRequestBehavior.AllowGet);

            if (string.IsNullOrEmpty(key))
                return Json(new { code = -1, msg = "请求鉴权失败" }, JsonRequestBehavior.AllowGet);
            if (key != "8858110")
                return Json(new { code = -2, msg = "请求鉴权失败" }, JsonRequestBehavior.AllowGet);
            if (string.IsNullOrEmpty(samplingStartTime))
                return Json(new { code = -4, msg = "请输入采样开始时间" }, JsonRequestBehavior.AllowGet);
            if (string.IsNullOrEmpty(samplingEndTime))
                return Json(new { code = -5, msg = "请输入采样结束时间" }, JsonRequestBehavior.AllowGet);
            //入厂煤
            var data = bLL.GetSamplingData("", samplingStartTime, samplingEndTime, cph, sparestr1);
            if (data == null)
                return Json(new { code = -99, msg = "没有检索到数据/数据尚未审核" }, JsonRequestBehavior.AllowGet);
            if (data.Rows.Count <= 0)
                return Json(new { code = -99, msg = "没有检索到数据/数据尚未审核" }, JsonRequestBehavior.AllowGet);
            var list = data.ToDataList<ErpSamplingDataModel>().Select(m => new
            {
                SAMPLING_BEGIN_TIME = m.SAMPLING_BEGIN_TIME.ToString("yyyy-MM-dd HH:mm:ss"),
                BATCH_TYPE = m.BATCH_TYPE == "自采煤" ? "其他" : m.BATCH_TYPE,
                SAMPLING_END_TIME = m.SAMPLING_END_TIME.ToString("yyyy-MM-dd HH:mm:ss"),
                MATCH_TIME = m.MATCH_TIME.ToString("yyyy-MM-dd HH:mm:ss"),
                m.BATCH_CODE,
                m.ISJSUAN,
                m.LPR,
                m.MAKE_CODE,
                m.MATERIEL_ID,
                m.MATERIEL_NAME,
                m.MATERIEL_TYPE_NAME,
                m.SUPPLIER_ID,
                m.SUPPLIER_NAME,
                m.TEST_CODE,
                m.TRANSPORT_ID,
                m.TRANSPORT_NAME,
                m.MINE_ID,
                m.MINE_NAME,
                m.SPARESTR1,
                m.RFID,
                AUDIT_TIME = m.AUDIT_TIME.ToString("yyyy-MM-dd HH:mm:ss"),
            }).ToList();

            return Json(list, JsonRequestBehavior.AllowGet);
        }

        public bool TableIsNull(DataTable dataTable)
        {
            if (dataTable == null)
                return true;
            if (dataTable.Rows.Count <= 0)
                return true;
            return false;
        }

        public string CoverDateStr(string dateTime)
        {
            try
            {
                var data = Convert.ToDateTime(dateTime);
                return data.AddDays(1).ToString("yyyy-MM-dd");
            }
            catch
            {
                return "";
            }
        }

        [HttpGet]
        public JsonResult GetSamplingSingle(string key, string sparestr1)
        {
            //return Json(new { code = -1, msg = "接口暂未开放" }, JsonRequestBehavior.AllowGet);

            if (string.IsNullOrEmpty(key))
                return Json(new { code = -1, msg = "请求鉴权失败" }, JsonRequestBehavior.AllowGet);
            if (key != "8858110")
                return Json(new { code = -2, msg = "请求鉴权失败" }, JsonRequestBehavior.AllowGet);
            if (string.IsNullOrEmpty(sparestr1))
                return Json(new { code = -4, msg = "请输入流程号" }, JsonRequestBehavior.AllowGet);

            DataTable data = bLL.GetSamplingData("入厂煤", "", "", "", sparestr1);
            if (TableIsNull(data))
            {
                //根据流程码查询  煤种名称，和入场日期
                var dataCarInfo = bLL.GetErpCarInfoFromSparestr1(sparestr1);
                if (!TableIsNull(dataCarInfo))
                {
                    var carInfo = dataCarInfo.ToDataList<ErpCarInfoModel>();
                    string specification = dataCarInfo.Rows[0]["specification"].ToString();
                    string firstdatetime = dataCarInfo.Rows[0]["firstdatetime"].ToString();
                    string likeCode = CoverDateStr(firstdatetime);
                    //return Json($"specification:{specification}   sparestr1:{sparestr1}  likeCode:{likeCode}", JsonRequestBehavior.AllowGet);
                    specification = specification == "浑源1-3焦煤" ? "浑源1/3焦煤" : specification;
                    specification = specification == "K主焦" ? "K主焦煤" : specification;
                    specification = specification == "吕家坨二焦煤" ? "吕二焦煤" : specification;
                    specification = specification == "吕家坨三焦煤" ? "吕三焦煤" : specification;
                    specification = specification == "大同弱粘煤" ? "大同弱粘" : specification;
                    specification = specification == "哥伦比亚1/3焦煤" ? "哥伦比亚" : specification;
                    specification = specification == "兖矿2#气煤" ? "兖矿2气煤" : specification;
                    specification = specification == "唐山矿1-3焦煤" ? "唐山矿" : specification;
                    //范各庄焦煤 ？？？ 
                    //冶金焦[干熄]25-80mm ？？？
                    //峰景北瘦煤SS ？？？
                    //外样焦炭 ？？？
                    data = bLL.GetSamplingData("自采煤", "", "", "", "", specification, likeCode);
                    //var data2  = bLL.GetSamplingData("自采煤", "", "", "", "", "鸡西1/3焦煤", "2024-01-02");
                    //return Json($"specification:{specification}   sparestr1:{sparestr1}  likeCode:{likeCode}  data1:{data1.Rows.Count} data2:{data2.Rows.Count}", JsonRequestBehavior.AllowGet);
                    if (TableIsNull(data))
                        return Json($"specification:{specification}   sparestr1:{sparestr1}  likeCode:{likeCode}  data:{data.Rows.Count}", JsonRequestBehavior.AllowGet);
                }
                else
                {
                    //return Json("dataCarInfo is null", JsonRequestBehavior.AllowGet);
                }
            }

            if (TableIsNull(data))
                return Json(new { code = -97, msg = "没有检索到数据/数据尚未审核" }, JsonRequestBehavior.AllowGet);
            var list = data.ToDataList<ErpSamplingDataModel>().Select(m => new
            {
                SAMPLING_BEGIN_TIME = m.SAMPLING_BEGIN_TIME.ToString("yyyy-MM-dd HH:mm:ss"),
                SAMPLING_END_TIME = m.SAMPLING_END_TIME.ToString("yyyy-MM-dd HH:mm:ss"),
                MATCH_TIME = m.MATCH_TIME.ToString("yyyy-MM-dd HH:mm:ss"),
                m.BATCH_CODE,
                m.LPR,
                m.MAKE_CODE,
                m.MATERIEL_ID,
                m.MATERIEL_NAME,
                m.MATERIEL_TYPE_NAME,
                m.SUPPLIER_ID,
                m.SUPPLIER_NAME,
                m.TEST_CODE,
                m.TRANSPORT_ID,
                m.TRANSPORT_NAME,
                m.MINE_ID,
                m.MINE_NAME,
                SPARESTR1 = string.IsNullOrEmpty(m.SPARESTR1) ? sparestr1 : m.SPARESTR1,
                m.RFID,
                AUDIT_TIME = m.AUDIT_TIME.ToString("yyyy-MM-dd HH:mm:ss"),
            }).ToList();

            return Json(list, JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public JsonResult GeSqlserTest()
        {
            try
            {
                var ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnWlSystem"].ConnectionString;
                var data = bLL.GetErpSpecifications();
                if (data == null)
                    return Json(new { code = -99, msg = "没有检索到数据/数据尚未审核" }, JsonRequestBehavior.AllowGet);
                if (data.Rows.Count <= 0)
                    return Json(new { code = -99, msg = "没有检索到数据/数据尚未审核" }, JsonRequestBehavior.AllowGet);

                return Json(data.ToDataList<ErpSpecificationModel>(), JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return Json(new { code = -99, msg = ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }

        [HttpGet]
        public JsonResult GetTestData(string key, string testCode)
        {
            //return Json(new { code = -1, msg = "接口暂未开放" }, JsonRequestBehavior.AllowGet);
            if (string.IsNullOrEmpty(key))
                return Json(new { code = -98, msg = "请求鉴权失败" }, JsonRequestBehavior.AllowGet);
            if (key != "8858110")
                return Json(new { code = -99, msg = "请求鉴权失败" }, JsonRequestBehavior.AllowGet);
            if (string.IsNullOrEmpty(testCode))
                return Json(new { code = -100, msg = "请输入化验编码" }, JsonRequestBehavior.AllowGet);
            if (!bLL.IsJieSuan(testCode))
                return Json(new { code = -101, msg = "没有检索到数据/数据尚未审核" }, JsonRequestBehavior.AllowGet);
            var data = bLL.GetTestData(testCode);
            if (data == null)
                return Json(new { code = -102, msg = "没有检索到数据/数据尚未审核" }, JsonRequestBehavior.AllowGet);
            if (data.Rows.Count <= 0)
                return Json(new { code = -103, msg = "没有检索到数据/数据尚未审核" }, JsonRequestBehavior.AllowGet);
            var batchCode = data.Rows[0]["BATCH_CODE"].ToString();
            var yanXList = yxBll.GetAllList(batchCode);
            var relatedData = relatedBLL.Info(batchCode, "RC"); //关联表中的数据
            var list = data.ToDataList<ErpTestDataModel>();
            string yxValue = "";
            if (relatedData != null)
            {
                yxValue = relatedData.YX_VALUE;
                if (!string.IsNullOrEmpty(yxValue))
                    list.Add(new ErpTestDataModel
                    {
                        CONFIRMED_TIME = relatedData.ADDTIME,
                        TEST_CODE = testCode,
                        INDEX_DISNAME = "岩相反射率",
                        INDEX_NAME = "标准差",
                        TEST_VALUE = relatedData.YX_VALUE,
                        INDEX_CODE = "",
                        INDEX_ID = "",
                        INDEX_TYPE = "",
                        INDEX_UNIT = "%",
                    });
                if (!string.IsNullOrEmpty(relatedData.CRI))
                    list.Add(new ErpTestDataModel
                    {
                        CONFIRMED_TIME = relatedData.ADDTIME,
                        TEST_CODE = testCode,
                        INDEX_DISNAME = "焦炭反应性",
                        INDEX_NAME = "CRI",
                        TEST_VALUE = relatedData.CRI,
                        INDEX_CODE = "CRI",
                        INDEX_ID = "1032",
                        INDEX_TYPE = "焦炭",
                        INDEX_UNIT = "%",
                    });
                if (!string.IsNullOrEmpty(relatedData.CSR))
                    list.Add(new ErpTestDataModel
                    {
                        CONFIRMED_TIME = relatedData.ADDTIME,
                        TEST_CODE = testCode,
                        INDEX_DISNAME = "焦炭反应后强度",
                        INDEX_NAME = "CSR",
                        TEST_VALUE = relatedData.CSR,
                        INDEX_CODE = "CSR",
                        INDEX_ID = "1033",
                        INDEX_TYPE = "焦炭",
                        INDEX_UNIT = "%",
                    });
            }
            if (string.IsNullOrEmpty(yxValue))
                yanXList.ForEach(res =>
                {
                    list.Add(new ErpTestDataModel
                    {
                        CONFIRMED_TIME = res.ADDTIME,
                        TEST_CODE = res.TEST_CODE,
                        INDEX_DISNAME = "岩相反射率",
                        INDEX_NAME = "标准差",
                        TEST_VALUE = res.ITEM_VALUE,
                        INDEX_CODE = "",
                        INDEX_ID = "",
                        INDEX_TYPE = "",
                        INDEX_UNIT = "%",
                    });
                });
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].INDEX_CODE.ToUpper() == "B")
                    list[i].TEST_VALUE = list[i].TEST_VALUE == "300" ? "仅收缩" : list[i].TEST_VALUE;

                if (list[i].INDEX_CODE.ToUpper().Contains("MAD"))
                    list.Remove(list[i]);
                //list[i].TEST_VALUE = "";
                //if (list[i].INDEX_CODE == "mad")
                //    list[i].TEST_VALUE = "";
            }
            //mad 不对外
            var tolist = list.Select(m => new
            {
                CONFIRMED_TIME = m.CONFIRMED_TIME.ToString("yyyy-MM-dd HH:mm:ss"),
                m.TEST_CODE,
                m.INDEX_ID,
                m.INDEX_CODE,
                m.INDEX_NAME,
                m.INDEX_TYPE,
                m.INDEX_UNIT,
                m.INDEX_DISNAME,
                m.TEST_VALUE,
            }).ToList();

            return Json(list, JsonRequestBehavior.AllowGet);

        }
    }
}